package com.hyk.commonLib.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ResUtils;
import com.hyk.commonLib.common.utils.ScreenUtils;


/**
 * Created by 贺玉琨 on 2019/3/24.
 */
public class ClearableEditText extends AppCompatEditText {

    //按钮资源
    private final int CLEAR = R.drawable.common_lib_icon_edit_text_clear;
    //按钮左右间隔,单位DP
    private final int INTERVAL = 5;
    //清除按钮宽度,单位DP
    private final int WIDTH_OF_CLEAR = 23;


    //间隔记录
    private int interval;
    //清除按钮宽度记录
    private int mWidth_clear;
    //右内边距
    private int mPaddingRight;
    //清除按钮的bitmap
    private Bitmap mBitmap_clear;
    //是否显示清除按钮
    private boolean clearButtonVisible = false;
    //清除按钮的颜色
    @ColorInt
    private int clearBtnColor;

    //右边添加其他按钮时使用
    private int mRight = 0;

    private Paint paint;


    private DrawFilter drawFilter;

    public ClearableEditText(Context context) {
        super(context);
        init(context, null);
    }

    public ClearableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ClearableEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.ClearableEditText);
        clearButtonVisible = mTypedArray.getBoolean(R.styleable.ClearableEditText_showClear, false);
        clearBtnColor = mTypedArray.getColor(R.styleable.ClearableEditText_clearBtnColor, ResUtils.getColor(R.color.colorAccent));
        mTypedArray.recycle();

        //setSingleLine();这个方法不推荐写在代码中，原因请看博客尾部更新

        mBitmap_clear = createBitmap(CLEAR);

        interval = ScreenUtils.dp2px(INTERVAL);
        mWidth_clear = ScreenUtils.dp2px(WIDTH_OF_CLEAR);
        mPaddingRight = interval + mWidth_clear + interval;
        drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new PorterDuffColorFilter(clearBtnColor, PorterDuff.Mode.SRC_IN));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //设置内边距
        setPadding(getPaddingLeft(), getPaddingTop(), clearButtonVisible ? (mPaddingRight + mRight) : getPaddingRight(), getPaddingBottom());

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(drawFilter);//抗锯齿
        if (clearButtonVisible && getText().length() > 0) {
            drawClear(canvas);
        } else {
            drawClearGone(canvas);
        }
    }

    /**
     * 绘制清除按钮出现的图案
     *
     * @param canvas
     */
    protected void drawClear(Canvas canvas) {
        int right = getWidth() + getScrollX() - interval - mRight;
        int left = right - mWidth_clear;
        int top = (getHeight() - mWidth_clear) / 2;
        int bottom = top + mWidth_clear;
        Rect rect = new Rect(left, top, right, bottom);
        canvas.drawBitmap(mBitmap_clear, null, rect, paint);
    }

    /**
     * 绘制清除按钮消失的图案
     *
     * @param canvas
     */
    protected void drawClearGone(Canvas canvas) {
        int right = (int) (getWidth() + getScrollX() - interval - mRight - mWidth_clear / 2f);
        int left = (int) (getWidth() + getScrollX() - interval - mRight - mWidth_clear / 2f);
        int top = ((getHeight() - mWidth_clear) / 2);
        int bottom = (top + mWidth_clear);
        Rect rect = new Rect(left, top, right, bottom);
        canvas.drawBitmap(mBitmap_clear, null, rect, paint);
    }


    /**
     * 触控执行的监听
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            boolean touchable = clearButtonVisible && (getWidth() - interval - mRight - mWidth_clear < event.getX()) && (event.getX() < getWidth() - interval - mRight);
            if (touchable) {
                setError(null);
                this.setText("");
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 给图标染上当前提示文本的颜色并且转出Bitmap
     */
    protected Bitmap createBitmap(int resources) {
        return ResUtils.getBitmap(resources);
    }

    public void setClearButtonVisible(boolean clearButtonVisible) {
        this.clearButtonVisible = clearButtonVisible;
        invalidate();
    }

    public void setClearButtonIcon(Bitmap bitmap) {
        mBitmap_clear = bitmap;
        invalidate();
    }

}